﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RaytracerLib;
using RaytracerLib.MathLib;
using System.ComponentModel;
using RaytracerLib.RenderableObjects;
using System.Drawing;
using RaytracerLib.Attenuations;
using RaytracerLib.Materials;

namespace Raytracer.PropertyGridDecorators
{
    public class IntersectionProperties
    {
        private Intersection m_intersection;

        public IntersectionProperties(Intersection a_intersection)
        {
            m_intersection = a_intersection;
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3ReadonlyProperties Pos
        {
            get
            {
                return new Vector3ReadonlyProperties(m_intersection.Pos);
            }
        }

        public string SceneObject
        {
            get
            {
                return m_intersection.SceneObject.ToString();
            }
        }

        [TypeConverter(typeof(MaterialConverter))]
        [Category(Categories.PROPERTIES)]
        public Material Material
        {
            get
            {
                return m_intersection.Material;
            }
        }

        public double Dist
        {
            get
            {
                return m_intersection.Dist;
            }
        }

        public bool BackHit
        {
            get
            {
                return m_intersection.BackHit;
            }
        }

        public string ReflectedSideObject
        {
            get
            {
                if (m_intersection.ReflectedSideObject == null)
                    return "null";
                else
                    return m_intersection.ReflectedSideObject.ToString();
            }
        }

        public string RefractedSideObject
        {
            get
            {
                if (m_intersection.RefractedSideObject == null)
                    return "null";
                else
                    return m_intersection.RefractedSideObject.ToString();
            }
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3ReadonlyProperties Normal
        {
            get
            {
                return new Vector3ReadonlyProperties(m_intersection.Normal);
            }
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3ReadonlyProperties ShadeNormal
        {
            get
            {
                return new Vector3ReadonlyProperties(m_intersection.ShadeNormal);
            }
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3ReadonlyProperties UVW
        {
            get
            {
                return new Vector3ReadonlyProperties(m_intersection.UVW);
            }
        }

        [TypeConverter(typeof(Vector3Converter))]
        public Vector3ReadonlyProperties LocalPos
        {
            get
            {
                return new Vector3ReadonlyProperties(m_intersection.LocalPos);
            }
        }

        [TypeConverter(typeof(Vector2Converter))]
        public Vector2ReadonlyProperties UV
        {
            get
            {
                return new Vector2ReadonlyProperties(m_intersection.UV);
            }
        }

        [TypeConverter(typeof(Vector2Converter))]
        public Vector2ReadonlyProperties BaricentricUV
        {
            get
            {
                return new Vector2ReadonlyProperties(m_intersection.BaricentricUV);
            }
        }

        public double RefractionnIndiceN1
        {
            get
            {
                return m_intersection.RefractionIndiceN1;
            }
        }

        public double RefractionnIndiceN2
        {
            get
            {
                return m_intersection.RefractionIndiceN2;
            }
        }

        public Color SystemColor
        {
            get
            {
                return Gamma.LinearToSRGB(m_intersection.Color).SystemColor;
            }
        }

        [TypeConverter(typeof(ColorFloatConverter))]
        public ColorFloatReadonlyProperties Color
        {
            get
            {
                return new ColorFloatReadonlyProperties(m_intersection.Color);
            }
        }

        [TypeConverter(typeof(AttenuationConverter))]
        public AttenuationProperties Attenuation
        {
            get
            {
                if (m_intersection.InnerAttenuation == null)
                    return null;
                else if (m_intersection.InnerAttenuation is ExponentalAttenuation)
                {
                    return new ExponentalAttenuationProperties(
                        m_intersection.InnerAttenuation as ExponentalAttenuation);
                }
                else if (m_intersection.InnerAttenuation is PolynomialAttenuation)
                {
                    return new PolynomialAttenuationProperties(
                        m_intersection.InnerAttenuation as PolynomialAttenuation);
                }
                else
                    throw new NotImplementedException();
            }
        }

        #if DEBUG
        public long ID
        {
            get
            {
                return m_intersection.ID;
            }
        }
        #endif
    }
}
